﻿using AutoMapper;
using DevProtocolApi.Dtos.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.EntityModel.Auth;
using Service.IService.ISysService;

namespace DevProtocolApi.Controllers.sysControllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuController : BaseController
    {
        private readonly IMenuService _menuService;
        private readonly ILogger<MenuController> _logger;
        private readonly IMapper _mapper;

        public MenuController( IMenuService menuService, ILogger<MenuController> logger,IMapper mapper)
        {
            _menuService = menuService;
            _logger = logger;
            _mapper = mapper;
        }
        [HttpGet]
        [AllowAnonymous]
        public IActionResult GetAll()
        {
            try
            {
                var data = _menuService.GetAllMenus();
                //data = data.Where(item => item.name != "首页11").ToList();
                return data.Count > 0 ? Success(data) : Failed("查询失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询失败");
                return Failed($"添加失败: {ex.Message}");
            }
        }
        [HttpGet]
        public IActionResult GetMenusList()
        {
            try
            {
                var data = _menuService.GetMenusList();
                return data.Count > 0 ? Success(data) : Failed("查询失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询失败");
                return Failed($"添加失败: {ex.Message}");
            }
        }
        
        [HttpPost]
        public IActionResult AddMenu(MenuDto menu)
        {
            try
            {
                var _menu = _mapper.Map<Menu>(menu);
                bool res = _menuService.AddMenu(_menu);
                return res ? Success(res) : Failed("添加失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加失败");
                return Failed($"添加失败: {ex.Message}");
            }
        }
        [HttpPost]
        public IActionResult AddMenus(List<MenuDto> menus)
        {
            try
            {
                var _menus = _mapper.Map<List<Menu>>(menus);
                int res = _menuService.AddMenus(_menus);
                return res > 0 ? Success(res) : Failed("添加失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加失败");
                return Failed($"添加失败: {ex.Message}");
            }
        }
        [HttpPost]
        public IActionResult EditMenu(MenuDto menu)
        {
            try
            {
                var _menu = _mapper.Map<Menu>(menu);
                bool res = _menuService.EditMenu(_menu);
                return res  ? Success("编辑成功！") : Failed("编辑失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"编辑失败");
                return Failed($"编辑失败: {ex.Message}");
            }
        }
        [HttpDelete]
        public IActionResult DeleteMenus([FromBody] string[] ids)
        {
            try
            {
                int res = _menuService.DeleteMenus(ids);
                return res>0 ? Success("删除成功！") : Failed("删除失败！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"删除失败");
                return Failed($"删除失败: {ex.Message}");
            }
        }
        


    }
}
